package de.hdu.pms.ho.dao;

import org.junit.Test;

import de.hdu.pms.ho.model.Kategorie;
import de.hdu.pms.ho.model.Zimmer;

import java.util.List;



/**
 * Testclass to test all methods from {@link ZimmerDao}
 * @author Helena Semmel
 *
 */
public class ZimmerDaoTest extends AbstractDataAccessTest {


	private ZimmerDao zimmerDao;
	private String tableName = "Zimmer";

	public void setZimmerDao(ZimmerDao zimmerDao) {
		this.zimmerDao = zimmerDao;
	}
	
	private KategorieDao kategorieDao;
		public void setKategorieDao(KategorieDao kategorieDao) {
		this.kategorieDao = kategorieDao;
	}

	@SuppressWarnings("deprecation")
	@Test
	public void testFindById() {
		String[] tables = {tableName};
		deleteFromTables(tables);
		kategorieDao.create("Deluxe", 150);
		kategorieDao.create("Absteige", 15);
		List<Kategorie> allKat = kategorieDao.findAll();
		zimmerDao.create(1, 2, 25, true, false, allKat.get(0));
		zimmerDao.create(2, 4, 15, true, true, allKat.get(1));
		List<Zimmer> alleZimmer = zimmerDao.findAll();
		for (Zimmer z : alleZimmer) {
			int id = z.getId();
			Zimmer found = zimmerDao.findById(id);
			assertEquals(z.getNr(), found.getNr());
		}
		deleteFromTables(tables);
	}
	
	@Test
	public void testFindAll() {
		// already tested by testFindById()
		testFindById();
	}
	
	@SuppressWarnings("deprecation")
	@Test
	public void testCreate() {
		String[] tables = {tableName};
		deleteFromTables(tables);
		kategorieDao.create("Deluxe", 150);
		List<Kategorie> allKat = kategorieDao.findAll();
		zimmerDao.create(1, 2, 25, true, false, allKat.get(0));
		int rows = countRowsInTable(tableName);
		assertEquals("Die DB muss eine Zeile enthalten", 1, rows);
	}

	/**
	 * 
	 */
	@SuppressWarnings("deprecation")
	@Test
	public void testModify() {
		String[] tables = {tableName};
		deleteFromTables(tables);
		
		kategorieDao.create("Deluxe", 150);
		List<Kategorie> allKat = kategorieDao.findAll();
		zimmerDao.create(1, 2, 25, true, false, allKat.get(0));	
		
		sessionFactory.getCurrentSession().clear();
		List<Zimmer> alleZimmer = zimmerDao.findAll();
		Integer neueqm = 30;
		for (Zimmer x : alleZimmer) {
			int id = x.getId();
			zimmerDao.modify(id, 1, 2, neueqm, true, false, allKat.get(0));
		}
		alleZimmer = zimmerDao.findAll();
		Zimmer found = null;
		for (Zimmer x : alleZimmer) {
			found = x;
		}
		assertEquals(neueqm, found.getQm());
		deleteFromTables(tables);
	}
	
	@SuppressWarnings("deprecation")
	@Test
	public void testDeletebyId() {
		String[] tables = {tableName};
		deleteFromTables(tables);
		kategorieDao.create("Deluxe", 150);
		List<Kategorie> allKat = kategorieDao.findAll();
		zimmerDao.create(1, 2, 25, true, false, allKat.get(0));
		sessionFactory.getCurrentSession().flush();
		List<Zimmer> alleZimmer = zimmerDao.findAll();
		for (Zimmer x : alleZimmer) {
			int id = x.getId();
			zimmerDao.deletebyId(id);
			sessionFactory.getCurrentSession().flush();
			assertEquals(0, countRowsInTable(tableName));
		}
	}
	
}
